<?php

namespace App\Dao\Sign;

use App\Constants\SignTask;
use App\Dao\Order\OtherOrderDao;
use App\Dao\User\UserSpreadDao;
use Yj\Daos\BaseDaos;
use App\Model\Sign\SignTaskLog;
use Hyperf\Database\Model\Builder;
use Yj\Daos\Verify;
use Hyperf\Utils\Collection;

/**
 * @var SignTaskLog
 * @method $this params($params)
 */
class SignTaskLogDao extends BaseDaos
{
    public function searchParams(Collection $params)
    {
            $search = $this->getSearch();
            $params->check('id',function($id) use ($search) {
                $search->where('id',$id);
            });
            return $search;
    }

    public function lists(Collection $params)
    {
            $data = $this->params($params)->orderByDesc("id")->getList();
            return $data;
    }

    public function add(Collection $params)
    {
            $data = $this->create($params->toArray());
            return $data;
    }

    public function edit(int $id, Collection $params)
    {
            $data = $this->params(_Collect(["id"=>$id]))->firstOrError();
            $data->fill($params->toArray());
            return $data->save();
    }

    public function del(int $id)
    {
            $data = $this->params(_Collect(["id"=>$id]))->firstOrError();
            return $data->delete();
    }



    public function getFinishedNum(mixed $user_id,$code)
    {
        switch ($code){
            case SignTask::MEMBER:
                App(OtherOrderDao::class)
                    ->params(_Collect(['uid'=>$user_id]))
                    ->where('paid',1)
                    ->where('is_free',0)
                    ->count();
                break;
            case SignTask::SPREAD:
                /** @var UserSpreadDao $userSpreadDao */
                $userSpreadDao = App(UserSpreadDao::class);
                $count = $userSpreadDao->where('uid',$user_id)->yjRangeTime('spread_time','today')->count();
                break;
            default:
                $count =  $this->where("user_id", $user_id)->where('code',$code)->yjRangeTime('create_time','today')->count();
            break;
        }
        return $count;
    }

}
